package com.example.springvue.service.Impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.springvue.common.jwt.Md5Utils;
import com.example.springvue.common.utils.PageUitl;
import com.example.springvue.common.utils.CommonUtils;
import com.example.springvue.mapper.AdminUserMapper;
import com.example.springvue.model.AdminUser;
import com.example.springvue.service.AdminUserService;
import com.example.springvue.service.AuthGroupAccessService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AdminUserServiceImpl implements AdminUserService {
    @Resource
    private  AdminUserMapper adminUserMapper;

    @Resource
    private AuthGroupAccessService authGroupAccessService;
    @Override
    public AdminUser getAdminUserInfo(String username) {
        AdminUser adminUser = adminUserMapper.getAdminUserInfoModel(username);
        return adminUser;
    }

    @Override
    public AdminUser getAdminUserInfoId(int Id) {
        AdminUser adminUser = adminUserMapper.getAdminUserInfoId(Id);
//        QueryWrapper<AdminUser> adminUserQueryWrapper = new QueryWrapper<>();
//        LambdaQueryWrapper<AdminUser> like = adminUserQueryWrapper.lambda().eq(AdminUser::getId,Id);
//        AdminUser adminUser = adminUserMapper.selectOne(like);
        return adminUser;

    }

    @Override
    public Map<String, Object> getAdminUserList(int pageSize, int page) {
        PageHelper.startPage(page,pageSize);
        List<Map<String ,Object>> authGroup = adminUserMapper.getAdminUserList();

        //拼接头像
        authGroup = authGroup.stream().map((item) -> {
          Map<String,Object> map = item;
            map.put("avatar", CommonUtils.image((String) item.get("avatar")));
            item = map;
            return item;
        }).collect(Collectors.toList());

        Map<String ,Object> list = PageUitl.page(authGroup,pageSize,page);
        return list;
    }


    @Override
    @Transactional
    public boolean getAdminUserAdd(Map<String, Object> map) {
        String salt = Md5Utils.getSalt(6);//密码盐
        String password = Md5Utils.getMd5Code((String) map.get("password")+salt);
        AdminUser adminUser =new AdminUser();
        adminUser.setUsername((String) map.get("username"));
        adminUser.setAvatar((String) map.get("avatar"));
        adminUser.setEmail((String) map.get("email"));
        adminUser.setSort(1000);
        adminUser.setStatus(1);
        adminUser.setNickname((String) map.get("nickname"));
        adminUser.setPassword(password);
        adminUser.setSalt(salt);
        adminUser.setCreatetime(new Timestamp(System.currentTimeMillis()));
        adminUser.setUpdatetime(new Timestamp(System.currentTimeMillis()));
        int add = adminUserMapper.insert(adminUser);
        if (add==1){
            //插入角色绑定
            boolean AGA = authGroupAccessService.Add(adminUser.getId(),(Integer) map.get("group_id"));
            return true;
        }
        return false;
    }


    @Override
    @Transactional
    public boolean AdminUserEdit(Map<String, Object> map) {
        AdminUser adminUser =new AdminUser();
        adminUser.setId((Integer) map.get("id"));
        adminUser.setUsername((String) map.get("username"));
        adminUser.setAvatar((String) map.get("avatar"));
        adminUser.setEmail((String) map.get("email"));
        adminUser.setNickname((String) map.get("nickname"));
        if (map.containsKey("status")) {
            adminUser.setStatus((Integer) map.get("status"));
        }
        if (map.containsKey("sort")) {
            adminUser.setSort((Integer) map.get("sort"));
        }
        if (map.containsKey("password")&&map.get("password")!=null) {
            String password = Md5Utils.getMd5Code((String) map.get("password")+(String) map.get("salt"));
            adminUser.setPassword(password);
        }
        UpdateWrapper<AdminUser> adminUserUpdateWrapper = new UpdateWrapper<>();
        LambdaUpdateWrapper<AdminUser> eq = adminUserUpdateWrapper.lambda().in(AdminUser::getId,adminUser.getId());
        adminUser.setUpdatetime(new Timestamp(System.currentTimeMillis()));
        int Edit = adminUserMapper.update(adminUser,eq);
        if (Edit==1){
            if (map.containsKey("group_id")) {
                boolean AGA = authGroupAccessService.Edit(adminUser.getId(), (Integer) map.get("group_id"));
            }
            return true;
        }
        return false;
    }


    @Override
    @Transactional
    public boolean AdminUserDelete(String[] id) {
        UpdateWrapper<AdminUser> adminUserUpdateWrapper = new UpdateWrapper<>();
        LambdaUpdateWrapper<AdminUser> eq = adminUserUpdateWrapper.lambda().in(AdminUser::getId,id);
        int delete = adminUserMapper.delete(eq);
        boolean AGA = authGroupAccessService.delete(id);
        return true;
    }
}
